var a = require("../../../component/WxNotificationCenter/WxNotificationCenter.js"), e = getApp(), t = require("../../../utils/cityList.js");

Page({
    data: {
        avatar: "/images/avatar.png",
        roleCn: "主持人",
        role: 2,
        name: "",
        province: "",
        city: "",
        gender: 1,
        area: "",
        localAvatar: !1,
        phone: "",
        loading: !1
    },
    clickCity: function(a) {
        wx.navigateTo({
            url: "/pages/me/editInfo/editCity/editCity?backResult=1"
        });
    },
    clickGender: function(a) {
        this.setData({
            gender: 1 == this.data.gender ? 0 : 1
        });
    },
    clickAvatar: function(a) {
        wx.chooseImage({
            count: 1,
            sizeType: [ "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(a) {
                var e = a.tempFilePaths[0];
                wx.navigateTo({
                    url: "uploadAvatar/uploadAvatar?src=" + e
                });
            }
        });
    },
    bindinputName: function(a) {
        this.setData({
            name: a.detail.value
        });
    },
    clickRign: function(a) {
        if (1 != this.data.loading) {
            var t = this.data, o = t.name, n = t.province;
            t.city, t.area, t.password;
            "/images/avatar.png" != t.avatar ? 0 != o.length ? 0 != n.length ? (this.setData({
                loading: !0
            }), 1 == this.data.localAvatar ? this.registerInfoAvatar() : this.registerInfoWithWechatAvatar()) : e.util.showToast("请先设置头像") : e.util.showToast("请先设置姓名") : e.util.showToast("请先设置头像");
        }
    },
    registerInfoWithWechatAvatar: function() {
        var t = this, o = this.data, n = o.name, i = o.province, r = o.city, s = o.area, c = (o.password, 
        {
            name: n,
            password: "",
            phone: o.phone,
            sex: o.gender,
            province: i,
            city: r,
            county: s,
            role: o.role,
            programOpenId: wx.getStorageSync(e.storage.openid),
            weixinAvatar: this.data.avatar
        });
        e.globalData.weddingOrderId.length > 0 && (c.weddingOrderId = e.globalData.weddingOrderId), 
        e.globalData.inviteCode.length > 0 && (c.inviteCode = e.globalData.inviteCode), 
        e.network.post({
            url: e.url.registerInfoAvatarNoImage,
            params: c,
            before: function() {
                wx.showLoading({
                    title: "正在注册"
                }), wx.showNavigationBarLoading();
            },
            success: function(t) {
                wx.hideLoading(), e.util.showToast(t.data.message), 1 == t.data.result && (e.globalData.accessToken = t.data.data.accessToken, 
                e.globalData.firstLogin = !0, e.globalData.weddingOrderId = "", e.globalData.inviteCode = "", 
                e.globalData.authToken = t.data.data.authToken, wx.setStorageSync(e.storage.accessToken, t.data.data.accessToken), 
                wx.setStorageSync(e.storage.role, t.data.data.role), wx.setStorageSync(e.storage.hunyu, t.data.data.hunyu), 
                wx.setStorageSync(e.storage.authToken, t.data.data.authToken), e.util.updateUserInfo(), 
                e.util.lastAccessServer(), setTimeout(function() {
                    a.postNotificationName("didLoginSuccess");
                    var e = getCurrentPages();
                    wx.navigateBack({
                        delta: e.length - 1,
                        success: function(a) {},
                        fail: function(a) {
                            wx.redirectTo({
                                url: "/pages/schedule/schedule"
                            });
                        }
                    });
                }, 1500));
            },
            fail: function() {
                e.hideLoading(), e.util.showToast("获取团队信息失败");
            },
            complete: function() {
                wx.hideNavigationBarLoading(), t.setData({
                    loading: !1
                });
            }
        });
    },
    registerInfoAvatar: function() {
        var a = this, t = this.data, o = t.name, n = t.province, i = t.city, r = t.area, s = (t.password, 
        t.phone), c = t.gender, l = t.role, d = wx.getStorageSync(e.storage.openid), g = this.data.avatar, u = {
            name: o,
            password: "",
            phone: s,
            sex: c,
            province: n,
            city: i,
            county: r,
            role: l,
            programOpenId: d
        };
        e.globalData.weddingOrderId.length > 0 && (u.weddingOrderId = e.globalData.weddingOrderId), 
        e.globalData.inviteCode.length > 0 && (u.inviteCode = e.globalData.inviteCode);
        e.network.uploadFile({
            url: e.url.RegisterInfoAvatar,
            filePath: g,
            name: "file",
            formData: u,
            before: function() {
                wx.showLoading({
                    title: "正在注册"
                });
            },
            success: function(e) {
                a.handleRegisetResult(e);
            },
            fail: function() {
                wx.hideLoading(), e.util.showToast("提交数据失败");
            },
            complete: function() {
                a.setData({
                    loading: !1
                });
            }
        });
    },
    handleRegisetResult: function(t) {
        wx.hideLoading(), e.util.showToast(t.message), 1 == t.result && (e.globalData.accessToken = t.data.accessToken, 
        e.globalData.firstLogin = !0, e.globalData.weddingOrderId = "", e.globalData.inviteCode = "", 
        e.globalData.authToken = t.data.data.authToken, wx.setStorageSync(e.storage.accessToken, t.data.accessToken), 
        wx.setStorageSync(e.storage.role, t.data.role), wx.setStorageSync(e.storage.hunyu, t.data.hunyu), 
        wx.setStorageSync(e.storage.authToken, t.data.data.authToken), e.util.updateUserInfo(), 
        e.util.lastAccessServer(), setTimeout(function() {
            a.postNotificationName("didLoginSuccess");
            var e = getCurrentPages();
            wx.navigateBack({
                delta: e.length - 1,
                success: function(a) {},
                fail: function(a) {
                    console.log("navigateBack fail:", a), wx.redirectTo({
                        url: "/pages/schedule/schedule"
                    });
                }
            });
        }, 1500));
    },
    onLoad: function(a) {
        console.log(a), this.setData({
            avatar: a.avatarUrl,
            gender: a.gender,
            role: a.role,
            roleCn: a.roleCn,
            phone: a.phone
        }), this.setCity(a);
    },
    bindGetUserInfo: function(a) {
        console.log(a.detail.userInfo);
        var e = a.detail.userInfo;
        wx.navigateTo({
            url: "../completeInfo/completeInfo?nickName=" + e.nickName + "&city=" + e.city + "&province=" + e.province + "&avatarUrl=" + e.avatarUrl + "&gender=" + (1 == e.gender ? "1" : "0")
        });
    },
    setCity: function(a) {
        var e = t.cityList.provinces.map(function(a, e, t) {
            return a.name;
        }), o = -1;
        if (a.city) for (var n = a.city, i = a.province, r = "", s = 0; s < e.length; s++) {
            var c = e[s];
            if (-1 != c.indexOf(i)) {
                i = c, console.log(i), s;
                for (var l = t.cityList.provinces[s].citys.map(function(a, e, t) {
                    return a.name;
                }), d = 0; d < l.length; d++) {
                    var g = l[d];
                    if (console.log(g), -1 != g.indexOf(n)) {
                        o = d, n = g, console.log(n), r = t.cityList.provinces[s].citys[d].countys[0];
                        break;
                    }
                }
                break;
            }
        }
        -1 != o && this.setData({
            city: n,
            province: i,
            area: r
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});